//
//  UIImage+Additions.h
//  DJAppModule
//
//  Created by Lynn_Lin on 2018/3/22.
//

#import <UIKit/UIKit.h>

typedef struct __DJUICornerInset
{
    CGFloat topLeft;
    CGFloat topRight;
    CGFloat bottomLeft;
    CGFloat bottomRight;
} DJUICornerInset;

typedef enum __DJUIImageGradientDirection
{
    DJUIImageGradientDirectionVertical    = 1,
    DJUIImageGradientDirectionHorizontal  = 2,
    DJUIImageGradientDirectionTopLeft2BottomRight = 3,//左上到右下渐变
} DJUIImageGradientDirection;

UIKIT_EXTERN const DJUICornerInset DJUICornerInsetZero;

UIKIT_STATIC_INLINE DJUICornerInset DJUICornerInsetMake(CGFloat topLeft, CGFloat topRight, CGFloat bottomLeft, CGFloat bottomRight)
{
    DJUICornerInset cornerInset = {topLeft, topRight, bottomLeft, bottomRight};
    return cornerInset;
}

UIKIT_STATIC_INLINE DJUICornerInset DJUICornerInsetMakeWithRadius(CGFloat radius)
{
    DJUICornerInset cornerInset = {radius, radius, radius, radius};
    return cornerInset;
}

UIKIT_STATIC_INLINE BOOL DJUICornerInsetEqualToCornerInset(DJUICornerInset cornerInset1, DJUICornerInset cornerInset2)
{
    return
    cornerInset1.topLeft == cornerInset2.topLeft &&
    cornerInset1.topRight == cornerInset2.topRight &&
    cornerInset1.bottomLeft == cornerInset2.bottomLeft &&
    cornerInset1.bottomRight == cornerInset2.bottomRight;
}

FOUNDATION_EXTERN NSString* NSStringFromDJUICornerInset(DJUICornerInset cornerInset);


@interface UIImage (Additions)
/**
 *  vImage模糊图片
 *
 *  @param image 原始图片
 *  @param blur  模糊数值(0-1)
 *
 *  @return 重新绘制的新图片
 */
+ (UIImage *)dj_blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur;

//按指定的颜色和size生成UIImage。
+(UIImage*)createImageWithColor:(UIColor*)color size:(CGSize)size DEPRECATED_MSG_ATTRIBUTE("Use dj_imageWithColor:size:");

//生成一张一像素的小图.在显示时，通过平铺或拉伸来显示。
+(UIImage *)createImageWithColor:(UIColor *)color  DEPRECATED_MSG_ATTRIBUTE("Use dj_imageWithColor:");

//按指定的rect裁剪UIImage
+ (UIImage *)clipsImage:(UIImage*)image toRect:(CGRect)rect DEPRECATED_MSG_ATTRIBUTE("Use dj_imageByCropToRect:");

+ (UIImage*)imageWithSize:(CGSize)size borderColor:(UIColor *)color borderWidth:(CGFloat)borderWidth;

+(UIImage*)placeHolderImageWithFrame:(CGRect)rect;

+(UIImage *)roundedRectangleWithSize:(CGSize)size radius:(CGFloat)radius color:(UIColor *)imgColor;
+(UIImage *)cornerRecttangleWithSize:(CGSize)size radius:(CGFloat)radius color:(UIColor *)imgColor corner:(UIRectCorner)corner;
+(UIImage *)cornerRecttangleWithSize:(CGSize)size radius:(CGFloat)radius colors:(NSArray <UIColor *>*)imgColors corner:(UIRectCorner)corner isHorizontal:(BOOL)isHorizontal;


#pragma mark - 根据颜色生成图片

/**
 根据颜色生成图片

 @param color 颜色
 @return 返回一个图片
 */
+ (UIImage*)dj_imageWithColor:(UIColor*)color;


/**
  根据颜色生成指定大小图片

 @param color 颜色
 @param size 大小
 @return 返回一个图片
 */
+ (UIImage*)dj_imageWithColor:(UIColor*)color size:(CGSize)size;


/**
 根据颜色生成图片

 @param color 颜色
 @param borderAttributes 边框属性
    EX:
    NSDictionary *borderAttributes = @{
        NSStrokeColorAttributeName : [UIColor grayColor],//边框颜色
        NSStrokeWidthAttributeName:@(3)};//边框宽度
 
 @param size 指定大小
 @return 返回一个图片
 */
+ (UIImage*)dj_imageWithColor:(UIColor*)color borderAttributes:(NSDictionary*)borderAttributes size:(CGSize)size;


/**
 根据颜色生成图片
 
 @param color 颜色
 @param borderAttributes 边框属性
 EX:
 NSDictionary *borderAttributes = @{
 NSStrokeColorAttributeName : [UIColor grayColor],//边框颜色
 NSStrokeWidthAttributeName:@(3)};//边框宽度
 
 @param size 指定大小
 @param cornerRadius 圆角大小
 @return 返回一个图片
 */
+ (UIImage*)dj_imageWithColor:(UIColor*)color borderAttributes:(NSDictionary*)borderAttributes size:(CGSize)size cornerRadius:(CGFloat)cornerRadius;


/**
 根据颜色生成图片
 
 @param color 颜色
 @param borderAttributes 边框属性
 EX:
 NSDictionary *borderAttributes = @{
 NSStrokeColorAttributeName : [UIColor grayColor],//边框颜色
 NSStrokeWidthAttributeName:@(3)};//边框宽度
 
 @param size 指定大小
 @param cornerInset 指定各个圆角大小
 @return 返回一个图片
 */
+ (UIImage*)dj_imageWithColor:(UIColor*)color borderAttributes:(NSDictionary*)borderAttributes size:(CGSize)size cornerInset:(DJUICornerInset)cornerInset;

/**
 生成渐变图片

 @param colors 渐变色数组
 EX:
 UIColor startColor = ...;UIColor endColor = ...;
 NSArray *colors = @[startColor,endColor];
 @param size 图片大小
 @param direction 渐变方向
 @return 返回一个图片
 */
+ (UIImage*)dj_imageWithGradient:(NSArray*)colors size:(CGSize)size direction:(DJUIImageGradientDirection)direction;


/**
 生成渐变图片

 @param colors colors 渐变色数组
 EX:
 UIColor startColor = ...;UIColor endColor = ...;
 NSArray *colors = @[startColor,endColor];
 @param size 图片大小
 @param direction 渐变方向
 @param cornerInset 指定圆角大小
 @return 返回一个图片
 */
+ (UIImage*)dj_imageWithGradient:(NSArray*)colors size:(CGSize)size direction:(DJUIImageGradientDirection)direction cornerInset:(DJUICornerInset)cornerInset;

#pragma mark - 图片圆角处理

/**
 图片圆角大小裁剪

 @param radius 圆角大小
 @return 返回一个新的图片
 */
- (UIImage *)dj_imageByRoundCornerRadius:(CGFloat)radius;


/**
 图片圆角大小裁剪与设置

 @param radius 圆角大小
 @param borderWidth 边框大小
 @param borderColor 边框颜色
 @return 返回一个新的图片
 */
- (UIImage *)dj_imageByRoundCornerRadius:(CGFloat)radius
                             borderWidth:(CGFloat)borderWidth
                             borderColor:(nullable UIColor *)borderColor;


/**
 图片圆角大小裁剪与设置

 @param radius  圆角大小
 @param corners 指定哪几个角需要裁剪
 @param borderWidth 边框大小
 @param borderColor 边框颜色
 @param borderLineJoin LineJoin连接方式，决定圆角的样式
 @return 返回一个新的图片
 */
- (UIImage *)dj_imageByRoundCornerRadius:(CGFloat)radius
                                 corners:(UIRectCorner)corners
                             borderWidth:(CGFloat)borderWidth
                             borderColor:(UIColor *)borderColor
                          borderLineJoin:(CGLineJoin)borderLineJoin;



/**
 根据指定的rect裁剪图片

 @param rect 裁剪区域
 @return 返回一个新的图片
 */
- (UIImage *)dj_imageByCropToRect:(CGRect)rect;


/**
 缩放图片，默认缩放模式

 @param size 指定大小
 @return 返回一个新的图片
 */
- (UIImage *)dj_imageByResizeToSize:(CGSize)size;


/**
 缩放图片

 @param size 指定大小
 @param contentMode 缩放模式
 @return 返回一个新的图片
 */
- (UIImage *)dj_imageByResizeToSize:(CGSize)size contentMode:(UIViewContentMode)contentMode;


/**
 在指定的矩形中绘制整个图像

 @param rect 指定矩形中
 @param contentMode 绘制模式
 @param clips 一个布尔值，用于确定内容是否限于矩形。
 */
- (void)dj_drawInRect:(CGRect)rect withContentMode:(UIViewContentMode)contentMode clipsToBounds:(BOOL)clips;


/**
 在图片的边缘插入指定颜色

 @param insets 每个边插入的值，可以为负数
 @param color 边缘颜色
 @return 返回一个新的图片
 */
- (UIImage *)dj_imageByInsetEdge:(UIEdgeInsets)insets withColor:(UIColor *)color;


#pragma mark - 图片旋转相关
///=============================================================================
/// @name 图片旋转相关
///=============================================================================

/**
 Returns a new rotated image (relative to the center).
 
 @param radians   Rotated radians in counterclockwise.⟲
 
 @param fitSize   YES: new image's size is extend to fit all content.
 NO: image's size will not change, content may be clipped.
 */
- (nullable UIImage *)dj_imageByRotate:(CGFloat)radians fitSize:(BOOL)fitSize;

/**
 Returns a new image rotated counterclockwise by a quarter‑turn (90°). ⤺
 The width and height will be exchanged.
 */
- (nullable UIImage *)dj_imageByRotateLeft90;

/**
 Returns a new image rotated clockwise by a quarter‑turn (90°). ⤼
 The width and height will be exchanged.
 */
- (nullable UIImage *)dj_imageByRotateRight90;

/**
 Returns a new image rotated 180° . ↻
 */
- (nullable UIImage *)dj_imageByRotate180;

/**
 Returns a vertically flipped image. ⥯
 */
- (nullable UIImage *)dj_imageByFlipVertical;

/**
 Returns a horizontally flipped image. ⇋
 */
- (nullable UIImage *)dj_imageByFlipHorizontal;
@end
